using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using ShoppingCenter.Backend.Api.Model;
using ShoppingCenter.Backend.Api.Params;
using ShoppingCenter.Backend.Api.Repository;
using ShoppingCenter.Backend.Api.Utils;

namespace ShoppingCenter.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]

    //订单控制器
    public class OrdersController : ControllerBase
    {
        private IConfiguration _configuration;

        //订单
        private IRepository<Order> _orderRepository;
        //  收货信息
        private IRepository<Receiving_information> _receivingInformationRepository;


        public OrdersController(IConfiguration configuration, IRepository<Order> orderRepository, IRepository<Receiving_information> receivingInformationRepository)
        {
            _configuration = configuration;
            _receivingInformationRepository = receivingInformationRepository;
            _orderRepository = orderRepository;
        }



        //获取所有订单
        public dynamic Get()
        {

            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            var orders = _orderRepository.Table.ToList();
            // 分页
            var order = orders.Skip((currentPage - 1) * pageSize).Take(pageSize); ;


            return new
            {
                Code = 1000,
                Data = new
                {
                    Data = orders,
                    Pager = new { currentPage, pageSize, totalSize = orders.Count() }
                },
                Msg = "获取所有订单成功"
            };
        }


        [HttpGet("{userId}")]
        //根据用户id获取订单
        public dynamic Get(int userId)
        {

            var currentUserOrder = _orderRepository.GetById(userId);

            if (currentUserOrder == null)
            {
                return new
                {
                    Code = 200,
                    Data = "",
                    Msg = "当前用户没有订单"
                };
            }

            return new
            {
                Code = 1000,
                Data = currentUserOrder,
                Msg = "获取指定用户订单成功"
            };


        }



        //根据id修改订单
        [HttpPut("{id}")]
        public dynamic Put(int id, CUR_Order updateOrder)
        {

            //获取当前订单的收货人、手机号、地址、详情地址
            var ConsigneeName = updateOrder.ConsigneeName;
            var Phone = updateOrder.Phone;
            var Receiving_address = updateOrder.Receiving_address;
            var detailedAddress = updateOrder.detailedAddress;

            if (string.IsNullOrEmpty(ConsigneeName) || string.IsNullOrEmpty(Phone) || string.IsNullOrEmpty(Receiving_address) || string.IsNullOrEmpty(detailedAddress))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "当前订单的收货人、手机号、地址、详情地址不能为空"
                };
            }

            //通过Id获取要修改的订单
            var order = _orderRepository.GetById(id);

            if (order == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "你要更新的订单不存在"
                };
            }

            //通过当前订单表的用户id 获取用户收货信息表
            var receivingInformation = _receivingInformationRepository.Table.ToList().Where(x => x.UserId == order.UserId).FirstOrDefault();

            receivingInformation.ConsigneeName = ConsigneeName;
            receivingInformation.Phone = Phone;
            receivingInformation.Receiving_address = Receiving_address;
            receivingInformation.DetailedAddress = detailedAddress;


            //更新收货信息
            _receivingInformationRepository.Update(receivingInformation);

            return new
            {
                Code = 1000,
                Data = receivingInformation,
                Msg = "更新收货信息成功"
            };

            // return JsonHelper.Serialize(res);

        }

    }
}